package com.sxt.sys.controller;

import java.util.*;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sxt.sys.domain.Notice;
import com.sxt.sys.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.TreeNode;
import com.sxt.sys.domain.Permission;
import com.sxt.sys.service.PermissionService;
import com.sxt.sys.vo.PermissionVo;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 老雷
 * @since 2019-09-21
 */
@RestController
@RequestMapping("/p/permission")
public class PermissionController {

	@Autowired
	private PermissionService permissionService;
	@Autowired
	private RoleService roleService;

	/**
	 * 根据用户ID查询percode
	 */
	@HystrixCommand
	@RequestMapping("gerPercodeByUserId")
	public List<String> gerPercodeByUserId(Integer userId){
		//根据用户ID查询percode
		//查询所有菜单
		QueryWrapper<Permission> qw=new QueryWrapper<>();
		//设置只能查询菜单
		qw.eq("type",Constast.TYPE_PERMISSION);
		qw.eq("available", Constast.AVAILABLE_TRUE);
		//根据用户ID+角色+权限去查询
		//根据用户ID查询角色
		List<Integer> currentUserRoleIds = roleService.queryUserRoleIdsByUid(userId);
		//根据角色ID取到权限和菜单ID
		Set<Integer> pids=new HashSet<>();
		for (Integer rid : currentUserRoleIds) {
			List<Integer> permissionIds = roleService.queryRolePermissionIdsByRid(rid);
			pids.addAll(permissionIds);
		}
		List<Permission> list=new ArrayList<>();
		//根据角色ID查询权限
		if(pids.size()>0) {
			qw.in("id", pids);
			list=permissionService.list(qw);
		}
		List<String> percodes=new ArrayList<>();
		for (Permission permission : list) {
			percodes.add(permission.getPercode());
		}
		return percodes;
	}

	/**************** 权限管理开始 ****************/

	/**
	 * 加载权限管理左边的权限树的json
	 */
	@HystrixCommand
	@RequestMapping("loadPermissionManagerLeftTreeJson")
	public DataGridView loadPermissionManagerLeftTreeJson(PermissionVo permissionVo) {
		List<TreeNode> treeNodes = permissionService.loadPermissionManagerLeftTreeJson(permissionVo);
		return new DataGridView(treeNodes);
	}

	/**
	 * 查询
	 */
	@HystrixCommand
	@RequestMapping("loadAllPermission")
	public DataGridView loadAllPermission(PermissionVo permissionVo) {
		IPage<Permission> page = permissionService.loadAllPermission(permissionVo);
		return new DataGridView(page.getTotal(), page.getRecords());
	}

	/**
	 * 加载最大的排序码
	 * 
	 * @param
	 */
	@HystrixCommand
	@RequestMapping("loadPermissionMaxOrderNum")
	public Map<String, Object> loadPermissionMaxOrderNum() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Permission> list = this.permissionService.loadPermissionMaxOrderNum();
		if (list.size() > 0) {
			map.put("value", list.get(0).getOrdernum() + 1);
		} else {
			map.put("value", 1);
		}
		return map;
	}

	/**
	 * 添加
	 * 
	 * @param permissionVo
	 * @return
	 */
	@HystrixCommand
	@RequestMapping("addPermission")
	public ResultObj addPermission(PermissionVo permissionVo) {
	if(this.permissionService.addPermission(permissionVo)){
			return ResultObj.ADD_SUCCESS;
		} else {
			return ResultObj.ADD_ERROR;
		}
	}

	/**
	 * 修改
	 * 
	 * @param permissionVo
	 * @return
	 */
	@HystrixCommand
	@RequestMapping("updatePermission")
	public ResultObj updatePermission(PermissionVo permissionVo) {
		if(this.permissionService.updatePermission(permissionVo)){
			return ResultObj.UPDATE_SUCCESS;
		} else {
			return ResultObj.UPDATE_ERROR;
		}
	}

	/**
	 * 删除
	 * 
	 * @param permissionVo
	 * @return
	 */
	@HystrixCommand
	@RequestMapping("deletePermission")
	public ResultObj deletePermission(PermissionVo permissionVo) {
		if(this.permissionService.deletePermission(permissionVo)){
			return ResultObj.DELETE_SUCCESS;
		} else {
			return ResultObj.DELETE_ERROR;
		}
	}

	/**************** 权限管理结束 ****************/

}
